package com.xuzhijie.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Notice;
import com.xuzhijie.pms.entity.vo.NoticeVo;
import com.xuzhijie.pms.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-13
 */
@RestController
@ResponseBody
@RequestMapping("/notice")
public class NoticeController {


    @Autowired
    private INoticeService service;

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam(required = false) Date startTime,
                           @RequestParam(required = false) Date endTime,
                           @RequestParam String status) {

        return service.findPage(page, size, startTime, endTime,status);
    }

    @PutMapping("/create")
    public Result addNotice(@RequestBody NoticeVo notice){
        return service.addNotice(notice);
    }

    @GetMapping("/getNew")
    public Result getNew(){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("status",1);
        List<Notice> list = service.list(queryWrapper);
        System.out.println(list);
        return Result.success(list.get(0));
    }

    @GetMapping("/getOne")
    public Result getOne(@RequestParam String id){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("id",id);
        queryWrapper.eq("status",1);
        Notice one = service.getOne(queryWrapper);
        return Result.success(one);
    }

    @GetMapping("/getAll")
    public Result getAll(){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("status",1);
        List<Notice> list = service.list(queryWrapper);
        List<Notice> newl = new ArrayList<>();
        if (list.size() > 30){
            for (int i = 0; i < list.size(); i++) {
                newl.add(list.get(i));
            }
            return Result.success(newl);
        }else{
            return Result.success(list);
        }

    }

}
